package com.guigu.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.dao.CheckGroupMapper;
import com.guigu.health.dao.CheckGroup_Keys_CheckItemMapper;
import com.guigu.health.dao.CheckItemMapper;
import com.guigu.health.pojo.*;
import com.guigu.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName:CheckGroupServiceImpl
 * @Description:
 * @Author:longtao
 * @Date:2021/3/23
 * @Version:1.0
 */
@Service(value = "checkGroupService")
public class CheckGroupServiceImpl implements CheckGroupService {

    public CheckGroup_Keys_CheckItemMapper checkItemMapper;
    @Autowired
    @Qualifier(value = "checkGroup_Keys_CheckItemMapper")
    public void setCheckItemMapper(CheckGroup_Keys_CheckItemMapper checkItemMapper) {
        this.checkItemMapper = checkItemMapper;
    }

    private CheckItemMapper checkItemMapper2;
    @Autowired
    @Qualifier("checkItemMapper")
    public void setCheckItemMapper(CheckItemMapper checkItemMapper) {
        this.checkItemMapper2 = checkItemMapper;
    }

    private CheckGroupMapper checkGroupMapper;
    @Autowired
    @Qualifier(value = "checkGroupMapper")
    public void setCheckGroupMapper(CheckGroupMapper checkGroupMapper) {
        this.checkGroupMapper = checkGroupMapper;
    }




    @Override
    public List<CheckGroupResult> queryCheckGroupAllList() {
        List<CheckGroupResult> results = checkItemMapper.queryGroupAllList();
        if (results!=null){
            return results;
        }
      return null;
    }

    @Override
    public List<CheckItem> queryCheckItemAll(Integer id) {
        List<CheckItem> checkItems=checkItemMapper.queryCheckItemAll(id);
        return checkItems;
    }

    @Override
    public CheckItem queryCheckItemID(Integer id) {
        return checkItemMapper2.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckItemDetails> queryCheckItemByAccount(String account) {
        List<CheckItemDetails> checkItems = checkItemMapper2.queryCheckItemByAccount(account);

        return checkItems;
    }

    @Override
    public PageInfo<CheckGroupCheckItem> queryAllList(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,2);
        List<CheckGroupCheckItem> list = checkGroupMapper.queryAllCheckGrouop(name);
        PageInfo<CheckGroupCheckItem> pageInfo=new PageInfo<CheckGroupCheckItem>(list);

        return pageInfo;
    }

    @Override
    @Transactional
    public boolean addCheckGroup(CheckGroup checkGroup) {
        boolean flag=false;
        int i = checkGroupMapper.insert(checkGroup);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean deleteCheckGroup(Integer id) {
        boolean flag=false;
        /**
         * 先删除它与套餐中的关系
         */
        checkGroupMapper.deleteCheckGroupKey(id);
        int i = checkGroupMapper.deleteByPrimaryKey(id);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean updateCheckGroup(CheckGroup checkGroup, CheckGroupExample example,int [] checkItemIds) {
        boolean flag=false;
        /**
         * 删除检查和套餐之间的关系
         */
        checkGroupMapper.deleteCheckGroupKey(checkGroup.getId());
        /**
         * 在进行添加
         */
        if (null!=checkItemIds){
            for (int i : checkItemIds) {
                /**
                 * 添加检查组和检查项中的关系
                 */
                checkGroupMapper.addCheckGroupKey(checkGroup.getId(),i);
            }
        }
        int i = checkGroupMapper.updateByExampleSelective(checkGroup, example);
        if (i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean batchAdd(List<CheckGroup_CheckItemKey> list) {
        boolean flag=false;
        Integer add = checkItemMapper.batchAdd(list);
        if (add==list.size()){
            flag=true;
        }
        return flag;
    }

    @Override
    public List<CheckGroup> queryAll() {
        return checkGroupMapper.selectByExample(null);
    }

    @Override
    public List<CheckItem> querAllCheckItem() {
        List<CheckItem> list = checkItemMapper2.selectByExample(null);
        return list;
    }

    /**
     * 根据检查id查询出要选中的检查项
     *
     * @param checkGroupId
     * @return
     */
    @Override
    public List<Integer> getCountCheckItemIds(int checkGroupId) {
        List<Integer> list = checkGroupMapper.getCheckItemIds(checkGroupId);
        return list;
    }

    /**
     * 编码名不一致
     *
     * @param code
     * @return
     */
    @Override
    public int count(String code) {
        return  checkGroupMapper.count(code);
    }
}
